package com.hh.admin.controller;

import com.hh.common.model.integral.PointsMallCategory;
import com.hh.common.page.CommonPage;
import com.hh.common.request.PageParamRequest;
import com.hh.common.request.StoreSeckillSearchRequest;
import com.hh.common.request.integral.StoreIntegralAddRequest;
import com.hh.common.response.CommonResult;
import com.hh.common.response.integral.IntegralProductInfoResponse;
import com.hh.common.response.integral.StoreIntegralResponse;
import com.hh.service.service.integral.PointsMallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("api/admin/store/integral")
public class StorePointsMallController {

    @Autowired
    private PointsMallService pointsMallService;


    /**
     * 分页显示商品积分产品表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:pointsMall:list')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreIntegralResponse>>  getList(@Validated StoreSeckillSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<StoreIntegralResponse> storeIntegralResponseCommonPage =
                CommonPage.restPage(pointsMallService.getList(request, pageParamRequest));
        return CommonResult.success(storeIntegralResponseCommonPage);
    }

    /**
     * 获取积分商品分类列表
     * @param request
     * @param pageParamRequest
     * @return
     */
    @RequestMapping(value = "/category/list", method = RequestMethod.GET)
    public CommonResult<List<PointsMallCategory>>  getPointsMallCategoryList(@Validated StoreSeckillSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(pointsMallService.getPointsMallCategoryList());
    }

    /**
     * 新增商品积分产品表
     * @param storeIntegralAddRequest 新增参数
     */
    @PreAuthorize("hasAuthority('admin:pointsMall:save')")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated StoreIntegralAddRequest storeIntegralAddRequest) {
        if (pointsMallService.saveIntegralGoods(storeIntegralAddRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改商品积分产品表
     * @param storeIntegralAddRequest 修改参数
     */
    @PreAuthorize("hasAuthority('admin:pointsMall:update')")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated StoreIntegralAddRequest storeIntegralAddRequest) {
        if (pointsMallService.updateIntegralGoods(storeIntegralAddRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询商品积分产品表信息
     * @param id Integer
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<IntegralProductInfoResponse> info(@RequestParam(value = "id") Integer id) {
        IntegralProductInfoResponse integralGoods = pointsMallService.getDetailAdmin(id);
        return CommonResult.success(integralGoods);
    }

    /**
     * 修改积分商品状态
     * @param id
     * @param status
     * @return
     */
    @PreAuthorize("hasAuthority('admin:pointsMall:update:status')")
    @RequestMapping(value = "/update/status", method = RequestMethod.POST)
    public CommonResult<String> updateStatus(@RequestParam @Validated Integer id, @RequestParam @Validated boolean status) {
        if (pointsMallService.updateIntegralGoodsStatus(id,status)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }
}
